<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <form>
        <div>
            <label>手机号：</label>
            <input type="text" id="phone">
            <span></span>
        </div>
        <div>
            <label>密码：</label>
            <input type="password" id="password">
            <span></span>
        </div>
        <div>
            <label>确认密码：</label>
            <input type="password" id="confirmPassword">
            <span></span>
        </div>
    </form>

    <script>
        phone.onblur = function (e) {

            valid(/^1[3-9]\d{9}$/, '手机号', e);
        }
        password.onblur = function (e) {

            valid(/^[a-zA-Z]\w{5,19}$/, '密码', e);
        }
        confirmPassword.onblur = function (e) {
            valid(password.value, '密码', e);
        }

        function valid(regExp, message, e) {
            const value = e.target.value;

            let isRight = false;

            if (typeof regExp == 'object') {
                // 进入if，则表示regExp接收到的是正则
                isRight = regExp.test(value)
            } else {
                // 进入else，则表示regExp接收到的是输入的密码
                isRight = (regExp === value)
            }

            if (isRight) {
                e.target.nextElementSibling.innerText = "正确";
                e.target.nextElementSibling.style.color = 'green';
                e.target.style.borderColor = "#ccc";
            } else {
                // 验证不通过
                e.target.nextElementSibling.innerText = message + "格式不正确";
                e.target.nextElementSibling.style.color = 'red';
                e.target.style.borderColor = "red";
            }
        }
    </script>
</body>

</html>